package com.tarena.lbs.basic.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.tarena.lbs.basic.dao.repository.AreaRepository;
import com.tarena.lbs.basic.pojo.param.AreaParam;
import com.tarena.lbs.basic.pojo.po.AreaPO;
import com.tarena.lbs.basic.pojo.vo.AreaVO;
import com.tarena.lbs.basic.service.AreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
public class AreaServiceImpl implements AreaService {
    @Autowired
    private AreaRepository rep;

    @Override
    public List<AreaVO> areaList(Integer parentId){
        List<AreaVO> vos =null;
        List<AreaPO> pos = rep.areaList(parentId);
        // 判断持久化对象列表是否不为空--不为空则将 po转换成vo
        if (CollectionUtils.isNotEmpty(pos)){
            vos=pos.stream().map(po->{
                AreaVO vo = new AreaVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        return vos;
    }
    ;
    @Override
    public List<AreaVO> cityList(AreaParam param){
        List<AreaVO> vos =null;
        List<AreaPO> pos = rep.cityList(param);
        // 判断持久化对象列表是否不为空--不为空则将 po转换成vo
        if (CollectionUtils.isNotEmpty(pos)){
            vos=pos.stream().map(po->{
                AreaVO vo = new AreaVO();
                if(po.getName().contains("市辖区")){
                    po.setName(po.getName().replace("市辖区",""));
                }
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        return vos;
    }

    @Override
    public List<AreaVO> regionTree(){
        //1、先查出省，然后再递归出省下的市，市下的区
        List<AreaPO> allPos = rep.areaAllList();

        List<AreaVO> allVos=null;
        // 判断持久化对象列表是否不为空--不为空则将 po转换成vo
        if (CollectionUtils.isNotEmpty(allPos)){
            allVos=allPos.stream().map(po->{
                AreaVO vo = new AreaVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        // 1. 获取所有省级地区（父ID为0的视为顶级节点）
        List<AreaVO> provinces = allVos.stream()
                .filter(area -> area.getParentId() == 0)
                .collect(Collectors.toList());
        for (AreaVO vo : provinces){
            setChildren(vo,allVos);
        }
        return provinces;
    }

    //递归组成省市区树
    private void setChildren(AreaVO parent,List<AreaVO> allAreas){
        List<AreaVO> children = allAreas.stream()
                .filter(area -> parent.getId().equals(area.getParentId()))
                .collect(Collectors.toList());
        // 设置子节点
        parent.setChildren(children);
        // 递归为每个子节点设置它们的子节点
        for(AreaVO child:children){
            setChildren(child,allAreas);
        }

    }
}
